package com.kevin.utils;

import com.kevin.model.Location;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author kevin
 * @version V1.0
 * @Description: TODO
 * @date 2019/2/13 17:46
 **/
public class MapUtils {
    /**
     * 根据经纬度点坐标，获取中心点
     * @return
     */
    public static Location getCenterPoint(List<Location> locations){
        int total = locations.size();
        double X = 0, Y = 0, Z = 0;
        for(Location location : locations){
            double lat, lon, x, y, z;
            lat = location.getLat() * Math.PI / 180;
            lon = location.getLon()* Math.PI / 180;
            x = Math.cos(lat) * Math.cos(lon);
            y = Math.cos(lat) * Math.sin(lon);
            z = Math.sin(lat);

            X += x;
            Y += y;
            Z += z;
        }

        X = X / total;
        Y = Y / total;
        Z = Z / total;
        double Lon = Math.atan2(Y, X);
        double Hyp = Math.sqrt(X * X + Y * Y);
        double Lat = Math.atan2(Z, Hyp);

        Location center = new Location();
        center.setLon(Lon * 180 / Math.PI);
        center.setLat(Lat * 180 / Math.PI);


        return center;
    }

    /**
     * 根据列表获取相邻点的所有距离加和
     * @return
     */
    public static BigDecimal getListDis(List<Location> locations){
        BigDecimal dis = new BigDecimal(0);

        for(int i=1;i<locations.size();i++){
            dis = dis.add(BigDecimal.valueOf(Double.valueOf(DistanceUtil.algorithm(locations.get(i-1).getLon(),locations.get(i-1).getLat(),locations.get(i).getLon(),locations.get(i).getLat()))));
        }

        return dis;
    }
}
